//
//  ViewController.m
//  XYPieChartDemo
//
//  Created by Cluy on 15/6/9.
//  Copyright (c) 2015年 RUNLIN. All rights reserved.
//

#import "ViewController.h"
#define Num 260
@interface ViewController ()

@end

@implementation ViewController

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.slices = @[@"15",@"25",@"20",@"20",@"30",@"150"];
    [self.pieChartRight setDelegate:self];
    [self.pieChartRight setDataSource:self];

    [self.pieChartRight setShowPercentage:NO];
    self.sliceColors =[NSArray arrayWithObjects:
                       [UIColor colorWithRed:40/255.0 green:155/255.0 blue:0/255.0 alpha:1],
                       [UIColor colorWithRed:12/255.0 green:155/255.0 blue:0/255.0 alpha:1],
                       [UIColor colorWithRed:30/255.0 green:195/255.0 blue:29/255.0 alpha:1],
                       [UIColor colorWithRed:62/255.0 green:173/255.0 blue:219/255.0 alpha:1],
                       [UIColor colorWithRed:55/255.0 green:66/255.0 blue:115/255.0 alpha:1],
                       [UIColor colorWithRed:68/255.0 green:141/255.0 blue:139/255.0 alpha:1],nil];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.pieChartRight reloadData];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}


- (IBAction)showSlicePercentage:(id)sender {
    UISwitch *perSwitch = (UISwitch *)sender;
    [self.pieChartRight setShowPercentage:perSwitch.isOn];
}

#pragma mark - XYPieChart Data Source

- (NSUInteger)numberOfSlicesInPieChart:(XYPieChart *)pieChart
{
    return self.slices.count;
}

- (CGFloat)pieChart:(XYPieChart *)pieChart valueForSliceAtIndex:(NSUInteger)index
{
    return [[self.slices objectAtIndex:index] intValue];
}

- (UIColor *)pieChart:(XYPieChart *)pieChart colorForSliceAtIndex:(NSUInteger)index
{
    return [self.sliceColors objectAtIndex:(index % self.sliceColors.count)];
}

#pragma mark - XYPieChart Delegate
- (void)pieChart:(XYPieChart *)pieChart didSelectSliceAtIndex:(NSUInteger)index
{
    NSLog(@"did select slice at index %ld",(unsigned long)index);
    float number =[[self.slices objectAtIndex:index]floatValue]/Num;
    self.selectedSliceLabel.text = [NSString stringWithFormat:@"%f",number *100];
}
@end
